
package com.yadong.numberguess;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class EasterEggsActivity extends Activity {
    private static final int WIDTH = 60;
    private LinearLayout mRootLayout;
    Random random = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRootLayout = new LinearLayout(this);
        mRootLayout.setOrientation(LinearLayout.VERTICAL);
        mRootLayout.setLayoutParams(
                new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        setContentView(mRootLayout);
        initContent();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int w_screen = dm.widthPixels;
        int h_screen = dm.heightPixels;
        new MyThread(w_screen / WIDTH, h_screen / WIDTH).start();
    }

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
           if (msg.what == 1) {
                int index1 = msg.arg1;
                int index2 = msg.arg2;
                boolean randomColor = (Boolean) msg.obj;
                ((LinearLayout) mRootLayout.getChildAt(index1))
                        .getChildAt(index2).setBackgroundColor(
                                randomColor ?Color.rgb(random.nextInt(255), random.nextInt(255),
                                        random.nextInt(255)) : 0xffffffff);
            }
        };
    };

    private void sendMessage2(int index1, int index2, boolean save) {
        Message message = handler.obtainMessage(1);
        message.arg1 = index1;
        message.arg2 = index2;
        if (save) {
            mPositionSave.add(new Position(index1, index2));
            message.obj = false;
        } else {
            message.obj = true;
        }
        message.sendToTarget();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private class Position {
        int x;
        int y;

        public Position(int x, int y) {
            this.x = x;
            this.y = y;
        }
        @Override
        public boolean equals(Object o) {
            if (!(o instanceof Position))
                return false;
            return this.x == ((Position)o).x && this.y == ((Position)o).y;
        }

        @Override
        public int hashCode() {
            return x + y;
        }
    }

    private ArrayList<Position> mPositionSave = new ArrayList<EasterEggsActivity.Position>();

    private class MyThread extends Thread {
        private int width;
        private int hight;

        public MyThread(int width, int hight) {
            this.width = width;
            this.hight = hight;
        }

        public void run() {
            int[][] map = new int[hight][width];

            all:while (true) {
                int round = 0;
                while (true) {
                    for (int i = round; i < width - round; i++) {
                        map[round][i] = 1;
                        sendMessage2(round, i, false);
                    }
                    for (int j = round + 1; j < hight - round; j++) {
                        map[j][width - round - 1] = 1;
                        sendMessage2(j, width - round - 1, false);
                    }
                    for (int k = width - round - 1; k >= round; k--) {
                        map[hight - round - 1][k] = 1;
                        sendMessage2(hight - round - 1, k, false);
                    }
                    for (int l = hight - round - 1; l > round; l--) {
                        map[l][round] = 1;
                        sendMessage2(l, round, false);
                    }
                    round++;
                    if (round >= (WIDTH / 2)) {
                        break;
                    }
                }

                mPositionSave.clear();
                int max = width * hight;
                while (true) {
                    while (true) {
                        int itemIndex = random.nextInt(max);
                        Position position = new Position(itemIndex / width, itemIndex % width);
                        if (mPositionSave.indexOf(position) == -1) {
                            sendMessage2(position.x, position.y, true);
                            break;
                        }
                    }
                    if (mPositionSave.size() == max) {
                        finish();
                        break all;
                    }
                }
            }
        }
    };

    private void initContent() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int w_screen = dm.widthPixels;
        int h_screen = dm.heightPixels;

        for (int j = 0; j < (h_screen / WIDTH); j++) {
            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setLayoutParams(
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            for (int i = 0; i < (w_screen / WIDTH); i++) {
                View view = new View(this);
                view.setLayoutParams(new LayoutParams(WIDTH, WIDTH));

                view.setBackgroundColor(Color.rgb(random.nextInt(255), random.nextInt(255),
                        random.nextInt(255)));
                linearLayout.addView(view);
            }
            mRootLayout.addView(linearLayout);
        }
    }

}
